.386
STACK 	SEGMENT USE16 STACK
        DB 200 DUP(0)
STACK 	ENDS

DATA	SEGMENT USE16	
	STACKBAK	DB  200 DUP(0)	
DATA	ENDS

CODE	SEGMENT USE16
	ASSUME  CS:CODE, SS:STACK, DS:DATA, ES:DATA
START:	MOV  AX, DATA
	MOV  DS, AX
	MOV	AX,1
	MOV	BX,2
	PUSH	AX
	PUSH	BX
	PUSH	AX
	PUSH	BX

	MINUTE		DW  07
	SECOND		DW  0	
	MARK		DB  0
	COUNT	DB	18	;计数
	MIN	DB	?,?,':'
	SEC	DB	?,?
	BUF_LEN=$-MIN		;信息长度,'$'表示当前这条指令在代码段中的偏移量。
	CURSOR	DW	?	;原光标位置
	OLD_INT	DW	?,?	;原INT 08H的中断矢量
	PUSH	DS
	PUSH	CS
	POP	DS
	MOV	AX,3508H
	INT	21H

	MOV	OLD_INT,BX
	MOV	OLD_INT+2,ES
	MOV	DX,OFFSET  NEW08H
	CMP	DX,BX
	JE	NEXT		;已安装中断程序
NEW_INT:
	MOV	AX,2508H
	INT	21H	
	
NEXT:
	LDS	DX,DWORD PTR OLD_INT
	MOV	AX,2508H
	INT	21H
	POP	DS
	MOV  AH, 4CH
	INT  21H	
NEW08H	PROC	
	PUSHF
	CALL	DWORD PTR CS:OLD_INT		;在CS里面寻找
	
	DEC	CS:COUNT
	JZ	DISP
	IRET
DISP:	MOV	CS:COUNT,18
	STI
	PUSHA
	PUSH	DS
	PUSH	ES

	MOV	AX, CS		;将DS数据段首址转化为CS寻址
	MOV	DS, AX
	MOV	ES, AX
	CALL	GET_TIME	;获取当前时间,转换成ASCII码
	MOV	BH, 0
	MOV	AH, 3
	INT	10H
	MOV	CURSOR, DX	;保存原光标位置
	MOV	BP, OFFSET  MIN	;信息起始地址
	MOV	BH,0
	MOV	DH,0
	MOV	DL,80-BUF_LEN
	MOV	BL,07H
	MOV	CX,BUF_LEN
	MOV	AL,0
	MOV	AH,13H		;调用显示字符串功能
	INT	10H		;右上角显示时间
	MOV	BH,0
	MOV	DX,CURSOR	;恢复原来光标位置
	MOV	AH,2
	INT	10H

	POP	ES
	POP	DS
	CALL	CHANGE
	POPA
	IRET
NEW08H	ENDP

CHANGE	PROC
	
	CMP	MARK,0
	JE	CHANGE0_1
	JNE	CHANGE1_0
CHANGE0_1:			;交换stack和stackbak
	MOV	BP,200
	MOV	BX,200
LOPA:	MOV	CL,BYTE PTR [BP]
	MOV	BYTE PTR [BX],CL
	DEC	BP
	DEC	BX
	CMP	BP,-1
	JNE	LOPA

	MOV	BX,DS
	MOV	SS,BX
	MOV	MARK,1
	JMP	EXITGE

CHANGE1_0:			;交换stackbak和stack
	MOV	BX,DS
	MOV	SS,BX		
	MOV	BP,200
	MOV	BX,200
LOPA7:	MOV	CL,BYTE PTR [BX]
	MOV	BYTE PTR [BP],CL
	DEC	BP
	DEC	BX
	CMP	BP,-1
	JNE	LOPA7

	MOV	MARK,0
EXITGE:	RET
CHANGE	ENDP

GET_TIME PROC

	MOV	AL,2
	OUT	70H,AL
	JMP	$+2
	IN	AL,71H
	MOV	AH,AL
	AND	AL,0FH
	SHR	AH,4
	ADD	AX,3030H
	XCHG	AH,AL
	MOV	WORD PTR MIN,AX

	MOV	AL,0
	OUT	70H,AL
	JMP	$+2
	IN	AL,71H
	MOV	AH,AL
	AND	AL,0FH
	SHR	AH,4
	ADD	AX,3030H
	XCHG	AH,AL
	MOV	WORD PTR SEC,AX
	RET
GET_TIME  ENDP

CODE	ENDS
	END  START
